package model.expression.command;


import java.util.List;

import model.expression.AbstractExpression;
import model.parser.ParserException;

import state.TabState;
import state.TurtleState;


public class SetXY extends AbstractCommandExpression{

	public SetXY(TabState ts) {
		super(ts);
	}


	@Override
	public int evaluate() {
		int firstActiveTurtleReturnState = 0;
		if (this.children.size()<2)
			throw ParserException.BAD_ARGUMENTS;
		int newX = (int) this.children.get(0).evaluate();
		int newY = (int) this.children.get(1).evaluate();
		
		// for each active turtle, evaluate
		for (int i = 0; i < tabSt.getActiveTurtle().size(); i++) {
			int turtleID = tabSt.getActiveTurtle().get(i);
			if (i == 0){
				tabSt.turtlesMap.get(turtleID).setx(newX);
				tabSt.turtlesMap.get(turtleID).sety(newY);
				firstActiveTurtleReturnState = tabSt.turtlesMap.get(turtleID).disMoved();
			}
			else{
				tabSt.turtlesMap.get(turtleID).setx(newX);
				tabSt.turtlesMap.get(turtleID).sety(newY);
			}
			tabSt.turtlesMap.get(turtleID).notifyObservers();
		}
		return firstActiveTurtleReturnState;
	}


	@Override
	protected int getExpNumChildren() {
		return 2;
	}


}
